# -*- coding:utf-8 -*-
import sys
sys.path.append("..")
import Cust.leancloudapi
GetApi = Cust.leancloudapi.GetApi()
Dictionary = GetApi.getDictionary()
class Zidian:
    def getAll(self):
      query = Dictionary.query
      query.limit(1000)
      res = query.find()
      zidian_list = []
      for item in res:
          id = item.id
          key = item.get('Key')
          value = item.get('Value')
          ztype = item.get('Type')
          zidianinfo = {'id':id,'key':key,'value':value,'type':ztype}
          zidian_list.append(zidianinfo)
      return zidian_list
    def getzidian(self,id):
      query = Dictionary()
      res = query.create_without_data(id)
      res.fetch()
      key = res.get('Key')
      value = res.get('Value')
      ztype = res.get('Type')
      zidianinfo = {'id':id,'key':key,'value':value,'type':ztype}
      return zidianinfo
    def insertzidan(self,ztype,key,value):
      zidian = Dictionary()
      zidian.set('Type',ztype)
      zidian.set('Key',key)
      zidian.set('Value',value)
      try:
          zidian.save()
          return 710
      except Exception as e:
          print e.message
          return 711
    def modifyzidian(self,id,ztype,key,value): 
      updatezi = Dictionary.create_without_data(id)
      updatezi.set('Type',ztype)
      updatezi.set('Key',key)
      updatezi.set('Value',value)
      try:
          updatezi.save()
          return 730
      except Exception as e:
          print e.message
          return 731
    def delzidain(self,id):
      delzidain = Dictionary.create_without_data(id)
      try:
          delzidain.destroy()
          return 720
      except Exception as e:
          print e.message
          return 721
